<?php

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

$unionid = empty($_POST['unionid'])?0:intval($_POST['unionid']);
$_GET['op'] = empty($_GET['op'])?'create2':$_GET['op'];
$_POST['refer'] = empty($_POST['refer'])?"space.php?do=home":$_POST['refer'];
$newunioninfoid = 0;

if($_GET['op'] == 'delete') {

	if(!empty($_GET['infoid']))
		$flag = $_SGLOBAL['db']->query("DELETE FROM ".tname('unioninfo')." WHERE id='".intval($_GET['infoid'])."' and uid='".$_SGLOBAL['supe_uid']."'");

} elseif($_GET['op'] == 'add') {

	if(submitcheck('addsubmit')) {

		if(!$unionid) {
			showmessage('do_success', $_POST['refer'], 0);
		}

		//新用户见习
		cknewuser();

		//判断是否操作太快
		$waittime = interval_check('post');
		if($waittime > 0) {
			showmessage('operating_too_fast', '', 1, array($waittime));
		}

		//获取心情
		$mood = 0;
		preg_match("/\[em\:(\d+)\:\]/s", $_POST['message'], $ms);
		$mood = empty($ms[1])?0:intval($ms[1]);

		$message = getstr($_POST['message'], 600, 1, 1, 1);

		//替换表情
		$message = preg_replace("/\[em:(\d+):]/is", "<img src=\"image/face/\\1.gif\" class=\"face\">", $message);
		$message = preg_replace("/\<br.*?\>/is", ' ', $message);

		$setarr = array(
			'uid' => $_SGLOBAL['supe_uid'],
			'unionid' => $unionid,
			'dateline' => $_SGLOBAL['timestamp'],
			'content' => $message,
			'ip' => getonlineip()
		);

		//入库
		$newunioninfoid = inserttable('unioninfo', $setarr, 1);

		showmessage('do_success', $_POST['refer'], 0);
		exit();

		/*$_GET['op'] = "addsuccess";
		$str = "{id:$newunioninfoid,content:$message,uid:".$setarr['uid'].",dateline:".$setarr['dateline']."}";
		include_once template('cp_union');
		exit();*/

	}

} elseif($_GET['op'] == 'create'){

	if(submitcheck('createunionsubmit')) {

		//发送消息
		//$username = empty($_POST['username'])?'':$_POST['username'];

		$unionname = trim($_POST['unionname']);
		if(empty($unionname)) {
			showmessage('unable_to_send_air_news');
		}

		$setarr = array(
			'createuid' => $_SGLOBAL['supe_uid'],
			'unionname' => $unionname,
			'dateline' => $_SGLOBAL['timestamp'],
			'uids' => $_SGLOBAL['supe_uid'].','
		);
		//入库
		$newunionid = inserttable('union', $setarr, 1);
		$unionids = empty($space['unionids'])?$newunionid:$space['unionids'].",".$newunionid;
		updatetable('space', array('unionids'=>$unionids), array('uid'=>$_SGLOBAL['supe_uid']));
		$space = getspace($_SGLOBAL['supe_uid'], 'uid');
		showmessage('do_success', 'space.php?do=union&id='.$newunionid, 1);

	}

}

include template('cp_union');

?>